package com.hngy.xpq.shoppingcartmodule.ui.dashboard;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.hngy.xpq.shoppingcartmodule.R;
import com.hngy.xpq.shoppingcartmodule.adapter.CartAdapter;
import com.hngy.xpq.shoppingcartmodule.adapter.ListAdapter;
import com.hngy.xpq.shoppingcartmodule.bean.Cart;
import com.hngy.xpq.shoppingcartmodule.bean.Shop;
import com.hngy.xpq.shoppingcartmodule.database.SqlCart;
import com.hngy.xpq.shoppingcartmodule.database.SqlShop;
import com.hngy.xpq.shoppingcartmodule.tool.alg;

import java.util.List;
import java.util.Map;

public class DashboardFragment extends Fragment {

    private DashboardViewModel dashboardViewModel;
    private RecyclerView listCart;
    private Button cart_z_price;

    private SqlCart sqlCart;                // sqLite数据库 SqlCart

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        dashboardViewModel =
                ViewModelProviders.of(this).get(DashboardViewModel.class);
        View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
        // 初始化
        init(root);
        return root;
    }

    private void init(View root) {
        sqlCart = new SqlCart(root.getContext());
        listCart = root.findViewById(R.id.listCart);
        cart_z_price = root.findViewById(R.id.cart_z_price);
//        cartList = dashboardViewModel.getCartList(sqlCart, root);
        // 渲染结算
        cart_z_price = root.findViewById(R.id.cart_z_price);
        dashboardViewModel.getListChat(root).observe(getViewLifecycleOwner(), new Observer<List<Cart>>() {
            @Override
            public void onChanged(List<Cart> list) {
                // 数据渲染
                getCartAdapter(list);
                final Map map = alg.CountZPrice(list);
                cart_z_price.setText("结算: ￥ " + String.format("%.2f", map.get("price")));
                cart_z_price.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // 创建对话框
                        AlertDialog.Builder isExit=new AlertDialog.Builder(getActivity());
                        // 设置标题
                        isExit.setTitle("清单");
                        // 设置提示
                        isExit.setMessage("总金额：" + String.format("%.2f", map.get("z")) + "\n" +
                                "优惠金额：" + String.format("%.2f", map.get("youhui")) + "\n" +
                                "结算金额：" + String.format("%.2f", map.get("price")));
                        // 添加选择按钮并注册监听
                        isExit.setPositiveButton("支付",dListener);
                        isExit.setNegativeButton("取消",dListener);
                        // 提示框显示
                        isExit.show();
                    }
                });
            }
        });
    }

    DialogInterface.OnClickListener dListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int buttonId) {
            switch (buttonId) {
                case AlertDialog.BUTTON_POSITIVE:       // 确定支付操作
                    try {
                        Thread.sleep(1000);
                        sqlCart.delete();
                        dashboardViewModel.gengxing(getView());
                        Toast.makeText(getContext(), "自动支付成功", Toast.LENGTH_SHORT).show();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;
                case AlertDialog.BUTTON_NEGATIVE:     // 取消退出操作
                    break;
                default:
                    break;
            }
        }
    };

    // List适配器
    private void getCartAdapter(List<Cart> list) {
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
        listCart.setLayoutManager(layoutManager);
        CartAdapter adapter = new CartAdapter(list, cart_z_price);
        listCart.setAdapter(adapter);
    }
}